// FetionLoginBox.cpp
// Heihaier <admin@heihaier.org>
// http://www.heihaier.org

#include "FetionWindow.h"
#include "FetionLoginBox.h"
#include "main.h"

FetionLoginBox::FetionLoginBox()
{
	// Logo image
	Gtk::Image * pImage = Gtk::manage(new Gtk::Image("./image/theme/login_image-linux.gif"));
	pack_start(*pImage, TRUE, TRUE, 0);

	// ID label
	Gtk::Label * pLabel = Gtk::manage(new Gtk::Label("Mobile number or Fetion number"));
	pLabel->set_size_request(-1, 30);
	pack_start(*pLabel, FALSE, TRUE, 0);

	// ID entry
	Gtk::Entry * pEntry = Gtk::manage(new Gtk::Entry());
	m_pID = pEntry;
	pEntry->set_alignment(0.5);
	pack_start(*pEntry, FALSE, TRUE, 0);

	// Password label
	pLabel = Gtk::manage(new Gtk::Label("Fetion password"));
	pLabel->set_size_request(-1, 30);
	pack_start(*pLabel, FALSE, TRUE, 0);

	// Password entry
	pEntry = Gtk::manage(new Gtk::Entry());
	m_pPassword = pEntry;
	pEntry->set_alignment(0.5);
	pEntry->set_visibility(FALSE);
	pack_start(*pEntry, FALSE, TRUE, 0);

	// Login status hbox
	Gtk::HBox * pHBox = Gtk::manage(new Gtk::HBox());
	pack_start(*pHBox, FALSE, TRUE, 0);

	// Login status label
	pLabel = Gtk::manage(new Gtk::Label("Login status"));
	pLabel->set_alignment(0.0, 0.5);
	pLabel->set_size_request(-1, 30);
	pHBox->pack_start(*pLabel, FALSE, TRUE, 0);

	// Login status label
	pLabel = Gtk::manage(new Gtk::Label());
	m_pShow = pLabel;
	pLabel->set_size_request(-1, 30);
	pHBox->pack_start(*pLabel, TRUE, TRUE, 0);

	// Login button and status combobox hbox
	pHBox = Gtk::manage(new Gtk::HBox());
	pack_start(*pHBox, FALSE, TRUE, 0);

	// Status combobox
	Gtk::ComboBoxText * pComboBoxText = Gtk::manage(new Gtk::ComboBoxText());
	m_pStatus = pComboBoxText;
	pComboBoxText->append_text("Online");	// 0
	pComboBoxText->append_text("Busy");		// 1
	pComboBoxText->append_text("Away");		// 2
	pComboBoxText->append_text("Hide");		// 3
	pComboBoxText->set_active(0);
	pHBox->pack_start(*pComboBoxText, TRUE, TRUE, 0);

	// Login button image
	pImage = Gtk::manage(new Gtk::Image("./image/theme/Logion_In.gif"));

	// Login button
	Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
	m_pLogin = pButton;
	pButton->add(*pImage);
	pButton->set_relief(Gtk::RELIEF_NONE);
	pButton->signal_clicked().connect(sigc::mem_fun(this, &FetionLoginBox::on_login));
	pHBox->pack_start(*pButton, FALSE, TRUE, 0);

	// Remember password checkbutton
	Gtk::CheckButton * pCheckButton = Gtk::manage(new Gtk::CheckButton("Remember password"));
	m_pRemPWD = pCheckButton;
	pCheckButton->set_size_request(-1, 30);
	pack_start(*pCheckButton, FALSE, TRUE, 0);

	// Table
	Gtk::Table * pTable = Gtk::manage(new Gtk::Table(3, 2, FALSE));
	pTable->set_size_request(-1, 120);
	pack_start(*pTable, FALSE, TRUE, 0);

	// Report bugs linkbutton
	Gtk::LinkButton * pLinkButton = Gtk::manage(new Gtk::LinkButton("Report bugs"));
	pTable->attach(*pLinkButton, 1, 2, 0, 1);

	// About fetion linkbutton
	pLinkButton = Gtk::manage(new Gtk::LinkButton("About Fetion"));
	pTable->attach(*pLinkButton, 1, 2, 1, 2);

	// Network setting linkbutton
	pLinkButton = Gtk::manage(new Gtk::LinkButton("Network Setting"));
	pTable->attach(*pLinkButton, 1, 2, 2, 3);

	// LibFetion image
	pImage = Gtk::manage(new Gtk::Image("./image/theme/LibFetion.gif"));
	pTable->attach(*pImage, 0, 1, 2, 3);

	// connect signal_login_msg callback function
	signal_login_msg.connect(sigc::mem_fun(this, &FetionLoginBox::on_login_msg));

	show_all();
}

FetionLoginBox::~FetionLoginBox()
{
}

void FetionLoginBox::on_login()
{
	// Clear
	m_pShow->set_label("Logining...");
	if(m_pID->get_text_length() <= 0)
	{
		m_pShow->set_label("Please input ID!");
		return;
	}
	if(m_pPassword->get_text_length() <= 0)
	{
		m_pShow->set_label("Please input password!");
		return;
	}
	
	// Set login status
	switch(m_pStatus->get_active_row_number())
	{
	case 0:		// Online
		fx_set_login_status(FX_STATUS_ONLINE);
		break;
	case 1:		// Busy
		fx_set_login_status(FX_STATUS_BUSY);
		break;
	case 2:		// Away
		fx_set_login_status(FX_STATUS_AWAY);
		break;
	case 3:		// Hide
		fx_set_login_status(FX_STATUS_OFFLINE);
		break;
	}
	
	// Set login image
	m_pLogin->remove();
	Gtk::Image * pImage = Gtk::manage(new Gtk::Image("./image/theme/Login_Cancel.gif"));
	pImage->show();
	m_pLogin->add(*pImage);

	// Disconnect login button signal_clicked and connect to on_login_cancel
	m_pLogin->signal_clicked().connect(sigc::mem_fun(this, &FetionLoginBox::on_login_cancel));

	// Login
	fx_login(m_pID->get_text().data(), m_pPassword->get_text().data(), ::on_login_msg, this);
}

void FetionLoginBox::on_login_cancel()
{
	// close network
	fx_close_network();

	// Set login image
	m_pLogin->remove();
	Gtk::Image * pImage = Gtk::manage(new Gtk::Image("./image/theme/Logion_In.gif"));
	pImage->show();
	m_pLogin->add(*pImage);

	// Disconnect login button signal_clicked and connect to on_login_cancel
	m_pLogin->signal_clicked().connect(sigc::mem_fun(this, &FetionLoginBox::on_login));

	m_pShow->set_label("");
}

void FetionLoginBox::on_login_msg(int message, WPARAM wParam, LPARAM lParam)
{
	FetionWindow * pFetionWindow = NULL;

	switch(message)
	{
	case FX_LOGIN_CONNECTING:
		m_pShow->set_label("Connecting...");
		break;
	case FX_LOGIN_WAIT_AUTH:
		m_pShow->set_label("Wating auth...");
		break;
	case FX_LOGIN_AUTH_OK:
		break;
	case FX_LOGIN_FAIL:
		m_pShow->set_label("Login failed!");
		break;
	case FX_LOGIN_NETWORK_ERROR:
		m_pShow->set_label("Network error!");
		break;
	case FX_LOGIN_UNKOWN_ERROR:
		m_pShow->set_label("Unkown error!");
		break;
	case FX_LOGIN_UNKOWN_USR:
		m_pShow->set_label("Unkown user!");
		break;
	case FX_LOGIN_GCL_GETTING:
		break;
	case FX_LOGIN_GCL_OK:
		break;
	case FX_LOGIN_GCL_FAIL:
		m_pShow->set_label("GCL failed!");
		break;
	case FX_LOGIN_GP_GETTING:
		break;
	case FX_LOGIN_GP_OK:
		break;
	case FX_LOGIN_GP_FAIL:
		m_pShow->set_label("GP failed!");
		break;
	case FX_LOGIN_OK:
		m_pShow->set_label("Login successful!");
		pFetionWindow = (FetionWindow*)this->get_parent();
		if(pFetionWindow)
		{
			// Set system msg callback function
			fx_set_system_msg_cb(::on_system_msg, pFetionWindow);
			// emit signal_login_ok
			pFetionWindow->signal_login_ok.emit();
		}
		break;
	case FX_LOGIN_TIMEOUT:
		m_pShow->set_label("Timeout!");
		break;
	case FX_LOGIN_URI_ERROR:
		m_pShow->set_label("URI error!");
		break;
	case FX_LOGIN_SERVER_ERROR:
		m_pShow->set_label("Server error!");
		break;
	case FX_LOGIN_SERVER_REFUSE:
		m_pShow->set_label("Server refuse!");
		break;
	}
}

